Repository navigation
feat: let Binder read and write non-public bean classes - #27368
totally-not-ai[bot] wants to merge 10 commits into
Conversation
Type of change
|
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues block the changes enabling access to non-public beans and records.
No open issues 🎉
What changed in this PR
Enables Binder and other bean-property consumers, such as Grid, to access non-public bean classes without requiring applications to expose those types publicly.
Changes:
- Retries inaccessible getter and setter calls with reflective access enabled, with guidance when module restrictions prevent access.
- Enables reflective construction of non-public records in
writeRecord(). - Adds regression tests for non-public beans, nested properties, and record round trips.
| File | Description |
|---|---|
flow-data/src/test/java/com/vaadin/flow/tests/data/bean/NonPublicBeans.java |
Adds non-public bean and record fixtures outside the binder package. |
flow-data/src/test/java/com/vaadin/flow/data/binder/BinderTest.java |
Tests reading and writing a non-public record. |
flow-data/src/test/java/com/vaadin/flow/data/binder/BeanPropertySetTest.java |
Tests non-public bean accessors and nested properties. |
flow-data/src/main/java/com/vaadin/flow/data/binder/Binder.java |
Enables access to non-public record constructors. |
flow-data/src/main/java/com/vaadin/flow/data/binder/BeanPropertySet.java |
Retries inaccessible accessors and explains module-access failures. |
Review effort: Balanced
Also cover the error raised when the package is not opened.
|
Fixed the nested |
vaadin-review-bot
left a comment
There was a problem hiding this comment.
Reviewed the changes — left 2 comments.
| Finding | |
|---|---|
| 📐 | The new test asserts the exact wording of the exception message, against the testing convention. |
| 📐 | The new reflection helper is kept private in BeanPropertySet although ReflectTools already owns this pattern. |
|
|
||
| IllegalStateException exception = assertThrows( | ||
| IllegalStateException.class, () -> getter.apply(bean)); | ||
| assertEquals(String.format( |
There was a problem hiding this comment.
📐 The new test asserts the exact wording of the exception message, against the testing convention.
CONVENTIONS.md (Testing): "When a test checks an exception or log message … assert that it contains the parts that matter, such as the class name and the offending value, not the exact wording." Assert that the message contains the class name, the package name and the method name instead.
The case also leans on a JDK implementation detail (sun.nio.fs being neither exported nor opened), so pinning the full wording makes it doubly brittle.
BeanPropertySetTest.java:588 · conventions
There was a problem hiding this comment.
@vaadin-review-bot The test no longer pins the full wording. It now checks that the message names the method, the class, the package that is not opened, and points to module-info.java. It also expects IllegalArgumentException now, since the helper moved to ReflectTools (see the other thread). The exact message is checked in ReflectToolsTest against the constant, like the existing accessible-constructor test there.
| // S3011: bean types do not need to be public, e.g. a data class nested | ||
| // in a view, so their public accessors are not accessible as such | ||
| @SuppressWarnings("java:S3011") | ||
| private static void makeAccessible(Method method, |
There was a problem hiding this comment.
📐 The new reflection helper is kept private in BeanPropertySet although ReflectTools already owns this pattern.
CONVENTIONS.md (Public API): "A method that does not touch the state of the class it sits on belongs elsewhere … Search for an existing utility before writing a helper, and when the helper is genuinely new, put it in the util class it belongs to (ReflectTools, FrontendUtils, …) instead of keeping it private where it is needed first."
ReflectTools already carries the same "Its module does not open the package '%s' to Vaadin" wording in CREATE_INSTANCE_FAILED_INACCESSIBLE_CONSTRUCTOR and the same trySetAccessible guard in createInstanceWithAccessibleConstructor. Moving the helper there would also let writeRecord reuse it.
BeanPropertySet.java:570 · conventions
There was a problem hiding this comment.
@vaadin-review-bot Moved it to ReflectTools.makeAccessible(Executable), next to createInstanceWithAccessibleConstructor. It throws IllegalArgumentException like the other helpers there. BeanPropertySet calls it when a getter or setter call is denied, and writeRecord() now uses it too, but only when Binder cannot call the record constructor directly, so a record in a package exported only to Vaadin keeps working. A record in a package that is not opened now gets the descriptive message instead of the generic access error. I kept the existing constructor message separate: it gives instance-creation advice ("its no-arg constructor public") that does not fit getters, setters or record constructors.
writeRecord() now uses the same helper, so a record in a package that is not opened to Vaadin fails with the descriptive message instead of a generic access error. The tests assert the parts of the message that matter instead of its exact wording.
A public record whose package is exported only to Vaadin can be created directly, while trySetAccessible() fails for it. Check access first so such records keep working. Tests now also check the package and the fix-it hint in the message. Only the helper is tested for the package-not-open case; no JDK record in such a package fits a Binder test.
vaadin-review-bot
left a comment
There was a problem hiding this comment.
✅ Nothing to flag — the changes look good.
Getters and setters now check canAccess() and make the method accessible before the call, like writeRecord() does, instead of catching the IllegalAccessException and retrying. Also fixes the operator precedence and single-throwing-call lambda findings.
|
Fixed the three SonarCloud findings:
|
vaadin-review-bot
left a comment
There was a problem hiding this comment.
✅ Nothing to flag — the changes look good.
|
Fixed the remaining operator-precedence finding in |
|



Fixes #27367
missing API · flow-data · apps that bind
BinderorGridto a bean class or record that is not publicBinderfailed with anIllegalAccessExceptionwhen the bean class was not public, for example a data class nested privately in a form. Applications had to make such classes public only for data binding. NowBinder(and other users of the bean property set, such asGrid(Class)) read and write properties of non-public classes, andwriteRecord()creates non-public records.Risks:
ReflectToolsclass), no security, serialization, threading or performance concerns; public beans are still called directly; each accessor call adds one cheapcanAccess()check.Context. The accessors are public, but reflection still denies the call when the declaring class is not public. Other places in Flow already make members of application classes accessible the same way.
canAccess()first and make the accessor accessible only when needed, the same waywriteRecord()does. Public beans are called directly, as before.IllegalArgumentExceptionnames the member, class and package and says how to fix it.writeRecord()makes the record constructor accessible whenBindercannot call it directly, and fails with the same message in that case.ReflectTools.makeAccessible(Executable)helper (internal package), next to the existing accessible-constructor logic there.Chose this over the alternative of
MethodHandleslookups, which need the application to pass in a lookup and would add public API.